<template>
  <div class="login-container">
    <div class="login-box">
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" label-width="60px" class="login-form">
        <el-form-item label="账号" prop="username">
          <el-input v-model="loginForm.username"
                    prefix-icon="el-icon-s-custom" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              prefix-icon="el-icon-lock" clearable></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
        <el-form-item  label-width="0" style="text-align: center; text-decoration: none"><a @click="toAnother" href="javascript:;">点击进入中央认证登录入口</a></el-form-item>
      </el-form>
    </div>
  </div>

</template>

<script>
import {loginNet} from 'network/login'
import {endLoading, startLoading} from "@/network/loading";
import {loginWithToken} from "@/network/login";

export default {
  name: "Login",
  data() {
    return {
      token:'',
      showVerification:false,
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'}],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}],
      }
    }
  },
  methods:{
    toAnother(){
      window.location.href="https://cas.dgut.edu.cn?appid=javaee&redirect_uri=http://120.79.14.226:8080/login/dgut&&state=STATE";
    },
    loginFunction(){
      loginNet(this.loginForm).then(res => {
        if (res.code != 200) {
          this.$message.error({message: res.msg, duration: 1000});
          return;
        }
        const authority = res.data.authorities[0].authority;
        window.localStorage.setItem("springBoot-token", res.data.token);
        window.localStorage.setItem("role", authority);
        if (authority == "ROLE_user") {
          this.$router.push("/user");
        }
        if (authority == "ROLE_admin") {
          this.$router.push("/admin");
        }
      })
    },
    login(){
      this.$refs.loginFormRef.validate(valid=>{
        if(!valid) return false;
        this.loginFunction();
      })
    },
    resetLoginForm(){
      this.$refs.loginFormRef.resetFields();
    }
  }
}
</script>

<style scoped>
  .login-container{
    height: 100%;
    background-color: #2b4b6b;
  }
  .login-box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    display: flex;
    align-items: center;
    justify-content: center;
  }

</style>
